package com.example.client.controller;

import com.example.client.domain.Address;
import com.example.client.pojo.Result;
import com.example.client.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/client/addresses")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private HttpSession httpSession;

    @GetMapping
    public Result<List<Address>> selectAddresses() {
        String username = (String) httpSession.getAttribute("username");
        return addressService.getAddresses(username);
    }

    @PutMapping("/{address_id}")
    public Result updateAddresses(Address address, @PathVariable Integer address_id) {
        return addressService.updateAddressByAddressId(address, address_id);
    }

    @PostMapping
    public Result insertAddress(Address address) {
        //String username = "123";
        String username = (String) httpSession.getAttribute("username");
        return addressService.insertAddress(address, username);
    }

    @DeleteMapping("/{address_id}")
    public Result deleteAddresses(@PathVariable Integer address_id) {
        return addressService.deleteAddress(address_id);
    }
}
